package com.szt.mobiles.inventory.common.utils;

import android.os.Handler;
import android.os.Looper;

import com.szt.mobiles.inventory.app.HandsetApplication;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * @author 刘育成
 * @package com.lyc.xutilsdemo
 * @fileName Xutils
 * @date or 2019/1/7 9:37
 * @describe TODO
 */
public class Xutils {
    private volatile static Xutils instance;
    private Handler handler;

    private static final String CODE_PARAMS= "httpCode";
    private static final int SUCCESS = 200;//code返回成功；
    private static final int INTERNAL_SERVER_ERROR = 500;


    private Xutils() {
        handler = new Handler(Looper.getMainLooper());
    }

    /**
     * 单例模式
     *
     * @return
     */
    public static Xutils getInstance() {
        if (instance == null) {
            synchronized (Xutils.class) {
                if (instance == null) {
                    instance = new Xutils();
                }
            }
        }
        return instance;
    }
/**
 * 异步post请求
 *
 * @param url
 * @param json
 * @param callback
 */
public void post(String url, String json, final XCallBack callback){
    RequestParams params = new RequestParams(url);
    params.addHeader("Content type","application/json");
    params.setConnectTimeout(5*1000);
    params.setBodyContent(json);
    x.http().post(params, new Callback.CommonCallback<String>() {
        private String result=null;
        @Override
        public void onSuccess(String result) {
//            onSuccessResponse(result,callback);
            if (result!=null){
                this.result=result;
            }
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(CancelledException cex) {

        }

        @Override
        public void onFinished() {
            if (result!=null){
                JSONObject jsonObject=null;
                try {
                    jsonObject=new JSONObject(result);
                    int code=jsonObject.getInt(CODE_PARAMS);
                    if (code==SUCCESS){
                        onSuccessResponse(result,callback);
                    }else if (code==INTERNAL_SERVER_ERROR){
                        ToastUtils.getInstanc(HandsetApplication.getInstance()).showToast("内部服务器错误");
                        callback.onError();
                    }else {
                        ToastUtils.getInstanc(HandsetApplication.getInstance()).showToast("服务器错误码："+code);
                        callback.onError();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

        }
    });
}

    /**
     * 异步get请求返回结果,json字符串
     *
     * @param result
     * @param callBack
     */
    private void onSuccessResponse(final String result, final XCallBack callBack) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (callBack != null) {
                    callBack.onResponse(result);
                }
            }
        });
    }


    public interface XCallBack {
        void onResponse(String result);
        void onError();
    }
}
